package com.lindenvalley.ch.content;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

import com.lindenvalley.ch.R;

@Root(name="task")
public class Task implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Element(name="taskID")
	private int taskId;
	
	@Element
	private long date;
	
	@Element(name="tasktime", required=false)
	private String taskTime;
	
	@Element(name="taskname", required=false)
	private byte taskName;
	
	@Element(name="tasktext", required=false)
	private String taskText;
	
	@Element(name="berufsstext", required=false)
	private String berufssText;
	
	@Element(name="thementext", required=false)
	private String themenText;
	
	@Element
	private int status;
	
	@Element(required=false)
	private String place;
	
	private int activeTab = R.id.tatigkeitView;
	private final String DEFAULT_TIME = "0";
	private final String dateFormatPattern = "dd.MM.yyyy";
	private transient SimpleDateFormat mDateFormat;
	
	

	public Task() {
		super();
		init();
	}
	
	private void init(){
		mDateFormat = new SimpleDateFormat(dateFormatPattern);
	}

	public int getTaskId() {
		return taskId;
	}

	public void settaskID(int taskId) {
		this.taskId = taskId;
	}

	public void settaskID(String taskId) {
		try {
			this.taskId = Integer.parseInt(taskId);
		} catch (NumberFormatException ex) {
			ex.printStackTrace();
		}
	}

	public long getDate() {
		return date;
	}

	public void setdate(long date) {
		this.date = date;
	}
	
	public void setdate(String date) {
		try {
			if (mDateFormat == null){
				init();
			}
			this.date = mDateFormat.parse(date).getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getTaskTime() {
		return taskTime;
	}
	
	public String getTaskHour(){
		String result = DEFAULT_TIME;
		if (taskTime != null){
			String[] values = taskTime.split(":");
			if (values != null){
				result = values[0];
			}
		}
		
		return result;
		
	}
	
	public String getTaskMinutes(){
		String result = DEFAULT_TIME;
		if (taskTime != null){
			String[] values = taskTime.split(":");
			if (values != null && values.length == 2){
				result = values[1];
			}	
		}
		
		return result;
	}

	public void settasktime(String time) {
		this.taskTime = time;
	}

	public byte getTaskName() {
		return taskName;
	}

	public void settaskname(String taskName) {
		try {
			this.taskName = Byte.parseByte(taskName);
			this.taskName++;
		} catch (NumberFormatException ex) {
			ex.printStackTrace();
		}

	}

	public String getTaskText() {
		return taskText;
	}

	public void settasktext(String taskText) {
		if (taskText != null){
			this.taskText = taskText.replaceAll("\\\\n", String.valueOf('\n'));
		} else
			this.taskText = taskText;
//		this.taskText = taskText;
	}

	public int getStatus() {
		return status;
	}

	public void setstatus(int status) {
		this.status = status;
	}

	public void setstatus(String status) {
		try {
			this.status = Integer.parseInt(status);
		} catch (NumberFormatException ex) {
			ex.printStackTrace();
		}
	}

	public String getPlace() {
		return place;
	}

	public void setplace(String place) {
		this.place = place;
	}

	public void setberufsstext(String berufssText) {
//		if (berufssText != null){
//			this.berufssText = berufssText.replaceAll("\\\\n", String.valueOf('\n'));
//		} else
//			this.berufssText = berufssText;
		this.berufssText = berufssText;
	}

	public String getBerufssText() {
		return berufssText;
	}

	public void setthementext(String themenText) {
//		if (themenText != null){
//			this.themenText = themenText.replaceAll("\\\\n", String.valueOf('\n'));
//		} else
//			this.themenText = taskText;
		this.themenText = taskText;
	}

	public String getThemenText() {
		return themenText;
	}

	public boolean isEmpty() {
		return (taskText == null || taskText.length() == 0)
				&& (berufssText == null || berufssText.length() == 0)
				&& (themenText == null || themenText.length() == 0);
	}

	public void clear() {
		taskText = null;
		berufssText = null;
		themenText = null;
		place = null;

	}

	/**
	 * @param activeTab the activeTab to set
	 */
	public void setActiveTab(int activeTab) {
		this.activeTab = activeTab;
	}

	/**
	 * @return the activeTab
	 */
	public int getActiveTab() {
		return activeTab;
	}
	
	public String getBodyTextByActiveTab(int id){
		String result = null;
		switch (id) {
		case R.id.tatigkeitView:
			result = getTaskText();
			break;
		case R.id.berufsschuleView:
			result = getBerufssText();
			break;
		case R.id.themenView:
			result = getThemenText();
			break;
		}
		return result;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((berufssText == null) ? 0 : berufssText.hashCode());
		result = prime * result + ((place == null) ? 0 : place.hashCode());
		result = prime * result + taskId;
		result = prime * result
				+ ((taskText == null) ? 0 : taskText.hashCode());
		result = prime * result
				+ ((themenText == null) ? 0 : themenText.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Task other = (Task) obj;
		if (berufssText == null) {
			if (other.berufssText != null)
				return false;
		} else if (!berufssText.equals(other.berufssText))
			return false;
		if (place == null) {
			if (other.place != null)
				return false;
		} else if (!place.equals(other.place))
			return false;
		if (taskId != other.taskId)
			return false;
		if (taskText == null) {
			if (other.taskText != null)
				return false;
		} else if (!taskText.equals(other.taskText))
			return false;
		if (themenText == null) {
			if (other.themenText != null)
				return false;
		} else if (!themenText.equals(other.themenText))
			return false;
		return true;
	}
	
	

}
